﻿#include "xcast_handler.h"
#include "context.h"

using namespace xcast;

extern int xcast_handler_channel_stat_tips(void* user_data, xcast_data& e);

extern int xcast_handler_xcast_event(void* user_data, xcast_data& e);

extern int xcast_handler_channel_event(void* user_data, xcast_data& e);

extern int xcast_handler_stream_event(void* user_data, xcast_data& e);

extern int xcast_handler_device_event(void* user_data, xcast_data& e);

Context::Listener::Interface* g_XcastListener = nullptr;

namespace Context {
namespace Listener {
void Add(Interface* listener) {
  if (!listener)
    return;
  if (g_XcastListener)
    return;

  g_XcastListener = listener;
}

void Remove(Interface* listener) {
  if (!listener)
    return;
  if (g_XcastListener != listener)
    return;

  g_XcastListener = nullptr;
}
}
namespace XcastEvent {
void OnError(int error, const char* err_msg) {
  if (g_XcastListener) {
    g_XcastListener->OnError(error, err_msg);
  }
}

void OnTips(const char* tips) {
  if (g_XcastListener) {
    g_XcastListener->OnTips(tips);
  }
}
}

int StartUp(uint64_t uin, int32_t appid, bool test_env) {
  xcast_data settings;
  settings["test_env"] = false;//test_env;
  settings["conf_master"] = false;//true;

  int rt = xcast_core::startup(settings);
  if (rt == XCAST_OK) {
    xcast_core::handle_event(XC_EVENT_SYSTEM, xcast_handler_xcast_event, 0);
    xcast_core::handle_event(XC_EVENT_CHANNEL, xcast_handler_channel_event, 0);
    xcast_core::handle_event(XC_EVENT_STREAM, xcast_handler_stream_event, 0);
    xcast_core::handle_event(XC_EVENT_DEVICE, xcast_handler_device_event, 0);
    xcast_core::handle_event(XC_EVENT_STATISTIC_TIPS, xcast_handler_channel_stat_tips, 0);
  }
  return rt;
}


void ShutDown() {
  xcast_shutdown();
}
}
